package com.sleep.drug_traceability.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.sleep.drug_traceability.dao.mapper.DrugConMapper;
import com.sleep.drug_traceability.dao.mapper.DrugManMapper;
import com.sleep.drug_traceability.dao.mapper.DrugSelMapper;
import com.sleep.drug_traceability.dao.mapper.DrugTraMapper;
import com.sleep.drug_traceability.dao.pojo.*;
import com.sleep.drug_traceability.service.InfoService;
import com.sleep.drug_traceability.service.LoginService;
import com.sleep.drug_traceability.service.SubmitService;
import com.sleep.drug_traceability.vo.*;
import com.sleep.drug_traceability.vo.params.ConDrugParam;
import com.sleep.drug_traceability.vo.params.ManDrugParam;
import com.sleep.drug_traceability.vo.params.SelDrugParam;
import com.sleep.drug_traceability.vo.params.TraDrugParam;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author Sleep-6000
 * @date 2022/4/27
 */
@Service
@Transactional
public class SubmitServiceImpl implements SubmitService {

    @Autowired
    private LoginService loginService;
    @Autowired
    private InfoService infoService;
    @Autowired
    private DrugManMapper drugManMapper;
    @Autowired
    private DrugTraMapper drugTraMapper;
    @Autowired
    private DrugSelMapper drugSelMapper;
    @Autowired
    private DrugConMapper drugConMapper;

    @Override
    public Result submitManDrug(ManDrugParam manDrugParam, String token) {
        String name = manDrugParam.getName();
        String batchNum = manDrugParam.getBatchNum();
        String approvalNum = manDrugParam.getApprovalNum();
        String traceableCode = manDrugParam.getTraceableCode();
        Long mfd = manDrugParam.getMfd();
        Long exp = manDrugParam.getExp();
        Long std = manDrugParam.getStd();
        String other = manDrugParam.getOther();
        if (StringUtils.isBlank(name) || StringUtils.isBlank(batchNum)
                || StringUtils.isBlank(approvalNum) || StringUtils.isBlank(traceableCode)
                || mfd == 0 || exp == 0 || std == 0) {
            return Result.fail(ErrorCode.PARAMS_ERROR.getCode(), ErrorCode.PARAMS_ERROR.getMsg());
        }
        User user = loginService.checkToken(token);
        if (user == null) {
            return Result.fail(ErrorCode.TOKEN_ERROR.getCode(), ErrorCode.TOKEN_ERROR.getMsg());
        }

        DrugMan data = findDataByCode(traceableCode);
        if (data != null) {
            return Result.fail(ErrorCode.TRACEABLE_CODE_EXIT.getCode(), ErrorCode.TRACEABLE_CODE_EXIT.getMsg());
        }

        DrugMan drugMan = new DrugMan();
        drugMan.setName(name);
        drugMan.setBatchNum(batchNum);
        drugMan.setApprovalNum(approvalNum);
        drugMan.setTraceableCode(traceableCode);
        drugMan.setMfd(mfd);
        drugMan.setExp(exp);
        drugMan.setStd(std);
        drugMan.setOther(other);
        drugMan.setCreateDate(System.currentTimeMillis());
        drugMan.setUserId(user.getId());
        ManInfo manInfo = infoService.getManInfoByUserId(user.getId());
        drugMan.setManName(manInfo.getName());
        drugManMapper.insert(drugMan);
        return Result.success("submit success");
    }

    private DrugMan findDataByCode(String traceableCode) {
        LambdaQueryWrapper<DrugMan> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(DrugMan::getTraceableCode, traceableCode);
        wrapper.last("limit 1");
        return drugManMapper.selectOne(wrapper);
    }

    @Override
    public Result submitTraDrug(TraDrugParam traDrugParam, String token) {
        String drugName = traDrugParam.getDrugName();
        String manName = traDrugParam.getManName();
        String batchNum = traDrugParam.getBatchNum();
        String traceableCode = traDrugParam.getTraceableCode();
        Long ostd = traDrugParam.getOstd();
        String buyer = traDrugParam.getBuyer();
        String other = traDrugParam.getOther();
        if (StringUtils.isBlank(drugName) || StringUtils.isBlank(manName)
                || StringUtils.isBlank(batchNum) || StringUtils.isBlank(traceableCode)
                || StringUtils.isBlank(buyer) || ostd == 0) {
            return Result.fail(ErrorCode.PARAMS_ERROR.getCode(), ErrorCode.PARAMS_ERROR.getMsg());
        }
        User user = loginService.checkToken(token);
        if (user == null) {
            return Result.fail(ErrorCode.TOKEN_ERROR.getCode(), ErrorCode.TOKEN_ERROR.getMsg());
        }

        DrugTra drugTra = new DrugTra();
        drugTra.setDrugName(drugName);
        drugTra.setManName(manName);
        drugTra.setBatchNum(batchNum);
        drugTra.setTraceableCode(traceableCode);
        drugTra.setOstd(ostd);
        drugTra.setBuyerName(buyer);
        drugTra.setOther(other);
        drugTra.setIsArrive(0);
        drugTra.setUserId(user.getId());
        drugTra.setCreateDate(System.currentTimeMillis());

        drugTraMapper.insert(drugTra);
        return Result.success("submit success");
    }

    @Override
    public Result submitSelDrug(SelDrugParam selDrugParam, String token) {
        String drugName = selDrugParam.getDrugName();
        String manName = selDrugParam.getManName();
        String batchNum = selDrugParam.getBatchNum();
        String traceableCode = selDrugParam.getTraceableCode();
        Long ard = selDrugParam.getArd();
        String other = selDrugParam.getOther();
        if (StringUtils.isBlank(drugName) || StringUtils.isBlank(manName) || StringUtils.isBlank(batchNum)
                || StringUtils.isBlank(traceableCode) || ard == 0) {
            return Result.fail(ErrorCode.PARAMS_ERROR.getCode(), ErrorCode.PARAMS_ERROR.getMsg());
        }
        User user = loginService.checkToken(token);
        if (user == null) {
            return Result.fail(ErrorCode.TOKEN_ERROR.getCode(), ErrorCode.TOKEN_ERROR.getMsg());
        }

        DrugSel drugSel = new DrugSel();
        drugSel.setDrugName(drugName);
        drugSel.setManName(manName);
        drugSel.setBatchNum(batchNum);
        drugSel.setTraceableCode(traceableCode);
        drugSel.setArd(ard);
        drugSel.setOther(other);
        drugSel.setUserId(user.getId());
        drugSel.setCreateDate(System.currentTimeMillis());

        SelInfo selInfo = infoService.getSelInfoByUserId(user.getId());
        drugSel.setSeller(selInfo.getName());
        drugSelMapper.insert(drugSel);

        DrugTra updateDt = new DrugTra();
        updateDt.setIsArrive(1);
        LambdaUpdateWrapper<DrugTra> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(DrugTra::getBuyerName, selInfo.getName());
        wrapper.eq(DrugTra::getDrugName, drugName);
        wrapper.eq(DrugTra::getManName, manName);
        wrapper.eq(DrugTra::getBatchNum, batchNum);
        wrapper.eq(DrugTra::getTraceableCode, traceableCode);
        drugTraMapper.update(updateDt, wrapper);

        return Result.success("submit success");
    }

    @Override
    public Result submitConDrug(ConDrugParam conDrugParam, String token) {
        String consumer = conDrugParam.getConsumer();
        Integer sex = conDrugParam.getSex();
        String idNumber = conDrugParam.getIdNumber();
        String contact = conDrugParam.getContact();
        String drugName = conDrugParam.getDrugName();
        Long cond = conDrugParam.getCond();
        String batchNum = conDrugParam.getBatchNum();
        String traceableCode = conDrugParam.getTraceableCode();
        String other = conDrugParam.getOther();
        if (StringUtils.isBlank(consumer) || StringUtils.isBlank(idNumber)
                || StringUtils.isBlank(contact) || StringUtils.isBlank(drugName)
                || StringUtils.isBlank(batchNum) || StringUtils.isBlank(traceableCode)
                || cond == 0 || sex < 0 || sex > 1) {
            return Result.fail(ErrorCode.PARAMS_ERROR.getCode(), ErrorCode.PARAMS_ERROR.getMsg());
        }
        User user = loginService.checkToken(token);
        if (user == null) {
            return Result.fail(ErrorCode.TOKEN_ERROR.getCode(), ErrorCode.TOKEN_ERROR.getMsg());
        }

        DrugCon drugCon = new DrugCon();
        drugCon.setConsumer(consumer);
        drugCon.setSex(sex);
        drugCon.setIdNumber(idNumber);
        drugCon.setContact(contact);
        drugCon.setDrugName(drugName);
        drugCon.setCond(cond);
        drugCon.setBatchNum(batchNum);
        drugCon.setTraceableCode(traceableCode);
        drugCon.setOther(other);
        drugCon.setUserId(user.getId());
        drugCon.setCreateDate(System.currentTimeMillis());

        SelInfo selInfo = infoService.getSelInfoByUserId(user.getId());
        drugCon.setSeller(selInfo.getName());
        drugConMapper.insert(drugCon);

        return Result.success("submit success");
    }
}
